//
//  SurpriseShopDetailTopCell.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/8/14.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit
import SDWebImage

class SurpriseShopDetailTopCell: BaseTableViewCell {
    
    
    @IBOutlet weak var topImageView: UIImageView!
    
    @IBOutlet weak var bottomLabel: UILabel!
    
    @IBOutlet weak var topLabel: UILabel!
    
    @IBOutlet weak var imageViewH: NSLayoutConstraint!
    
    var model : SurpriseShopDetModel?{
        
        didSet{
            if model == nil {
                return
            }
            
            if let imageCach = SDWebImageManager.shared().imageCache {
                
                if let tempImage = imageCach.imageFromDiskCache(forKey: model!.goods_image)  {
                    
                    imageViewH.constant = tempImage.size.height / tempImage.size.width * KScreenW
                    
                    
                    topImageView.image = tempImage
                    
                }else{
                    
                    imageViewH.constant = KScreenW  / 16 * 9
                    
                    topImageView.sd_setImage(with: URL(string :model!.goods_image), placeholderImage: UIImage.defaulImage(), options: .progressiveDownload) { (image, _, _, _) in
                        
                    }
                    
                }
                
                
                
            }else{
                
                
                imageViewH.constant = (KScreenW - 20) / 16 * 9
                
                topImageView.sd_setImage(with: URL(string :model!.goods_image), placeholderImage: UIImage.defaulImage(), options: .progressiveDownload) { (image, _, _, _) in
                    
                }
                
                
            }
            
           
            bottomLabel.attributedText = attributedString(str: model!.goods_name, font:  UIFont.systemFont(ofSize: 15))
            

            
        }
        
        
    }
    
    
    
    
    func attributedString(str: String, font: UIFont) -> NSAttributedString? {
        
        
        // 根据原有字符串生成属性字符串
        let strM = NSMutableAttributedString(string: str)
        
        strM.addAttribute(NSFontAttributeName, value: font, range:NSMakeRange(0, str.characters.count))
        
        strM.addAttribute(NSForegroundColorAttributeName, value: UIColor.black, range:NSMakeRange(0, str.characters.count) )
        
        
        
        let paragr = NSMutableParagraphStyle.init()
        
        switch model!.od_state {
        case "1":
            topLabel.isHidden = true
        case "2":
            topLabel.isHidden = true
            
        case "3":
            topLabel.isHidden = false
            paragr.firstLineHeadIndent = 66;
            
        default:
            topLabel.isHidden = true
            
            
        }
        
        
        paragr.lineSpacing = 5;
        
        paragr.paragraphSpacing = 2;
        
        strM.addAttribute(NSParagraphStyleAttributeName, value: paragr, range: NSMakeRange(0, str.characters.count))
        
        
        
        return strM
        
    }
    
    


    override func awakeFromNib() {
        super.awakeFromNib()
        contentView.backgroundColor = UIColor.white
        
        topLabel.addCornerAndBorder(borderColor: nil, borderWidth: 0, masksToBounds: true, cornerRadius: 13)
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
